package common.commands;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import parser.AltaRicaParseModel;
import parser.AltaRicaParser;

public class LoadAltaRicaModel implements ICommand {

	@Override
	public String getName() {
		
		return "load";
	}

	@Override
	public String getSynopsis() {
		
		return "load <file.alt>";
	}

	@Override
	public CommandValue run(String arg) {
		
		FileInputStream fileIn = null;
        
		
		try {
				fileIn =  new FileInputStream(arg);
		} catch (FileNotFoundException e) {

				System.out.println("Unable to open file " + arg);
				return CommandValue.KO;
		}

		AltaRicaParseModel pmodel = AltaRicaParser.parseModel(fileIn);
		
		try {
				fileIn.close();
		} catch (IOException e) {
				System.out.println("Error: unable to close the file: " + arg);
		}
		
		pmodel.compileConsts();
		pmodel.compileDomains();
		pmodel.compileNodes();

		System.out.println("Model loaded");
		
		return CommandValue.OK;
	}

	@Override
	public String help() {
		
		return "Loads an AltaRica model";
	}

}
